package com.caiyi.financial.nirvana.sms.util;

import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DisturbUtil {

    private static Logger logger = LoggerFactory.getLogger(DisturbUtil.class);

    /**
     * 防止同一账户下同一个手机号发送频率过快
     *
     * @param smsUser
     * @param phone
     * @return
     */
    public static boolean isDisturbed(SmsUser smsUser, String phone) {
        try {
            String account = smsUser.getAccount();
            long dayCount = smsUser.getDayCount();
            long hourCount = smsUser.getHourCount();
            long minuteCount = smsUser.getMinuteCount();
            //获取当前时间
            LocalDateTime localDateTime = LocalDateTime.now();
            //组装一个以分钟发送条数的KEY
            String minute = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
            String minuteKey = String.format(JedisKey.SMS_ACCOUNT_PHONE_TIME_SEND_EXPIRE, account, phone, minute);
            //组装一个以小时发送条数的KEY
            String hour = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHH"));
            String hourKey = String.format(JedisKey.SMS_ACCOUNT_PHONE_TIME_SEND_EXPIRE, account, phone, hour);
            //组装一个以天发送条数的KEY
            String day = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            String dayKey = String.format(JedisKey.SMS_ACCOUNT_PHONE_TIME_SEND_EXPIRE, account, phone, day);

            int minuteExpireTime = 60 - localDateTime.getSecond() - 1;
            int hourExpireTime = (60 - localDateTime.getMinute() - 1) * 60 + minuteExpireTime;
            int dayExpireTime = (24 - localDateTime.getHour() - 1) * 60 * 60 + hourExpireTime;
            //获取当前key查看发送条数
            //发送短信条数加一，并设置过期时间为这分钟的完结时间
            //每次访问在redis中进行加一操作，日、时、分，都需要加1
            long minuteNum = JedisService.incr(minuteKey);
            if (minuteNum == 1) {
                JedisService.expire(minuteKey, minuteExpireTime);
            }
            if (minuteNum > minuteCount) {
                JedisService.decr(minuteKey);
                logger.info("DisturbUtil  : 每分钟发送次数：账号{}中手机号{}发送短信过于频繁。", account, phone);
                return false;
            }

            long hourNum = JedisService.incr(hourKey);
            if (hourNum == 1) {
                JedisService.expire(hourKey, hourExpireTime);
            }
            if (hourNum > hourCount) {
                JedisService.decr(hourKey);
                logger.info("DisturbUtil  : 每小时发送次数：账号{}中手机号{}发送短信过于频繁。", account, phone);
                return false;
            }

            long dayNum = JedisService.incr(dayKey);
            if (dayNum == 1) {
                JedisService.expire(dayKey, dayExpireTime);
            }
            if (dayNum > dayCount) {
                JedisService.decr(dayKey);
                logger.info("DisturbUtil  : 每天发送次数：账号{}中手机号{}发送短信过于频繁。", account, phone);
                return false;
            }
        } catch (Exception e) {
            logger.error("DisturbUtil  同一账户下同一个手机号发送频率过快处理异常!", e);
        }
        return true;
    }
}
